package com.link2room.cms.facade.impl;

import com.link2room.cms.dto.param.base.ArticleBaseParamDto;
import com.link2room.cms.dto.param.manager.ArticleManagerParamDto;
import com.link2room.cms.dto.result.ArticleBaseDto;
import com.link2room.cms.dto.result.ArticleDetailDto;
import com.link2room.cms.entity.ArticleBase;
import com.link2room.cms.entity.ArticleDetail;
import com.link2room.cms.exception.CmsException;
import com.link2room.cms.facade.IArticleFacadeService;
import com.link2room.cms.service.IArticleService;
import com.link2room.common.constant.DefaultValue;
import com.link2room.common.jpa.result.QueryResult;
import com.link2room.common.service.ICommonService;
import com.link2room.common.util.ClassConverUtil;
import com.link2room.common.util.L2RAssert;
import com.link2room.enums.exception.CmsSysExceptionEnum;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;
import java.util.UUID;

/**
 * @author sqj88
 * @date 2017/12/1 16:50
 */
@Component("articleFacadeService")
public class ArticleFacadeServiceImpl implements IArticleFacadeService {
    @Resource
    private ICommonService commonService;
    @Resource
    private IArticleService articleService;

    /**
     * 保存或更新文章
     * @param articleManagerParamDto
     * @return
     * @throws Exception
     */
    @Override
    public ArticleBaseDto saveOrUpdateArticle(ArticleManagerParamDto articleManagerParamDto)throws Exception{
        ArticleBaseDto articleBaseDto = articleManagerParamDto.getArticleBaseDto();
        if(articleBaseDto.getId()==null||new Long(0).equals(articleBaseDto.getId())){
            articleBaseDto.setArticleId(System.currentTimeMillis()+ "-"+UUID.randomUUID());
        }
        ArticleBase articleBase= ClassConverUtil.copyProperties(articleBaseDto, ArticleBase.class);
        ArticleDetail articleDetail=ClassConverUtil.copyProperties(articleBaseDto.getArticleDetailDto(), ArticleDetail.class);
        articleDetail.setArticleId(articleBase.getArticleId());
        if(Objects.equals(DefaultValue.T,articleBase.getAutoAudit())){
            articleDetail.setIsAudit(DefaultValue.T);
        }
        L2RAssert.stringBlank(articleBase.getLhotelGroupCode(), "articleBase-lhotelGroup");
        L2RAssert.stringBlank(articleBase.getCategoryCode(), "articleBase-categoryCode");
        L2RAssert.stringBlank(articleDetail.getLhotelGroupCode(), "articleDetail-lhotelGroup");
        articleService.saveOrUpdateArticle(articleBase,articleDetail);
        articleBaseDto=ClassConverUtil.copyPropertiesToDto(articleBase,ArticleBaseDto.class,false);
        articleBaseDto.setArticleDetailDto(ClassConverUtil.copyPropertiesToDto(articleDetail,ArticleDetailDto.class,false));
        return articleBaseDto;
    }

    /**
     * 列出文章列表
     * @param articleBaseParamDto
     * @return
     * @throws Exception
     */
    @Override
    public QueryResult<ArticleBaseDto> listArticles(ArticleBaseParamDto articleBaseParamDto)throws Exception{
        L2RAssert.stringBlank(articleBaseParamDto.getLhotelGroupCode(), "articleBase-lhotelGroupCode");
        //L2RAssert.stringBlank(articleBaseParamDto.getCategoryCode(), "articleBase-categoryCode");
        String lhotelGroupCode=articleBaseParamDto.getLhotelGroupCode();
        String categoryCode=articleBaseParamDto.getCategoryCode();
        return articleService.findArticleList(lhotelGroupCode,
                categoryCode,
                articleBaseParamDto.getIsHalt(),
                articleBaseParamDto.getIsAudit(),
                articleBaseParamDto.getLanguage(),
                articleBaseParamDto.getArticleType(),
                articleBaseParamDto.getKeyword(),
                articleBaseParamDto.getCurrentPage(),
                articleBaseParamDto.getPageSize());
    }

    /**
     * 根据ID获取文章信息
     * @param articleBaseParamDto
     * @return
     * @throws Exception
     */
    @Override
    public ArticleBaseDto getArticle(ArticleBaseParamDto articleBaseParamDto)throws Exception{
        L2RAssert.numberNull(articleBaseParamDto.getId(),"articleBase-Id");
        ArticleBase articleBase=commonService.findOneById(ArticleBase.class,articleBaseParamDto.getId());
        ArticleBaseDto articleBaseDto=ClassConverUtil.copyPropertiesToDto(articleBase,ArticleBaseDto.class,false);
        if(articleBase!=null){
            ArticleDetail queryAd=new ArticleDetail();
            queryAd.setLhotelGroupCode(articleBaseParamDto.getLhotelGroupCode());
            queryAd.setArticleId(articleBase.getArticleId());
            queryAd.setLanguage(articleBaseParamDto.getLanguage());
            ArticleDetail articleDetail=commonService.findOneEQ(ArticleDetail.class,queryAd);
            if(articleDetail!=null){
                articleBaseDto.setArticleDetailDto(ClassConverUtil.copyPropertiesToDto(articleDetail,ArticleDetailDto.class,false));
                try {
                    articleService.updateClickCount(articleDetail.getLhotelGroupCode(),articleDetail.getArticleId(),articleDetail.getLanguage());
                }catch (Exception e){}
            }else {
                throw new CmsException(CmsSysExceptionEnum.ARTICLE_DETAIL_IS_NOT_EXIST);
            }
        }else {
            throw new CmsException(CmsSysExceptionEnum.ARTICLE_BASE_IS_NOT_EXIST);
        }
        return articleBaseDto;
    }


    /**
     * 根据ID获取文章信息
     * @param articleBaseParamDto
     * @return
     * @throws Exception
     */
    @Override
    public ArticleBaseDto getArticleForManager(ArticleBaseParamDto articleBaseParamDto)throws Exception{
        L2RAssert.numberNull(articleBaseParamDto.getId(),"articleBase-Id");
        ArticleBase articleBase=commonService.findOneById(ArticleBase.class,articleBaseParamDto.getId());
        ArticleBaseDto articleBaseDto=ClassConverUtil.copyPropertiesToDto(articleBase,ArticleBaseDto.class,false);
        if(articleBase!=null){
            ArticleDetail queryAd=new ArticleDetail();
            queryAd.setLhotelGroupCode(articleBaseParamDto.getLhotelGroupCode());
            queryAd.setArticleId(articleBase.getArticleId());
            queryAd.setLanguage(articleBaseParamDto.getLanguage());
            ArticleDetail articleDetail=commonService.findOneEQ(ArticleDetail.class,queryAd);
            if(articleDetail!=null){
                articleBaseDto.setArticleDetailDto(ClassConverUtil.copyPropertiesToDto(articleDetail,ArticleDetailDto.class,false));
            }
        }else {
            throw new CmsException(CmsSysExceptionEnum.ARTICLE_BASE_IS_NOT_EXIST);
        }
        return articleBaseDto;
    }

    /**
     * 删除文章
     * @param articleBaseParamDto
     * @throws Exception
     */
    @Override
    public void deleteArticle(ArticleBaseParamDto articleBaseParamDto)throws Exception{
        L2RAssert.stringBlank(articleBaseParamDto.getLhotelGroupCode(), "articleBase-lhotelGroupCode");
        L2RAssert.numberNull(articleBaseParamDto.getId(),"articleBase-Id");
        articleService.deleteArticle(articleBaseParamDto.getLhotelGroupCode(),articleBaseParamDto.getId());
    }
}
